//table title
var pm_columnData = [
    {"Width": "25%", "ColumnName": "权限ID"},
    {"Width": "25%", "ColumnName": "权限Code"},
    {"Width": "25%", "ColumnName": "权限名称"},
    {"Width": "25%", "ColumnName": "操作"}
];

//table操作栏html
var pm_operateHtml = "<div class='form-inline'><button type='button' class='btn btn-primary innerButtons'" +
    " value='delete'>删除 </button> " +
    "<button type='button' class='btn btn-primary innerButtons' value='modify'>修改</button>" +
    " <button type='button' class='btn btn-primary innerButtons' data-toggle='modal' value='authorize'>授权</button>" +
    "</div>";

//授权table title
var pme_columnData = [
    {
        "Width": "10%",
        "ColumnName": "<input type='checkbox' id='title-permission-table-checkbox' name='title-table-checkbox'/>"
    },
    {"Width": "20%", "ColumnName": "菜单ID"},
    {"Width": "20%", "ColumnName": "菜单名称"},
    {"Width": "20%", "ColumnName": "菜单URL"}
];
//权限管理分页函数
var gotoPage = function (index) {
    var priviledgeCode = $("#permissionCode").val();
    var url = webPath + "/crm/getPriviledgeByPage.do";
    var postData = {"priviledgeCode": priviledgeCode, "pageSize": pageSize, "pageNumber": index}
    var options = {"passArguments": index};
    postAjax(url, postData, gotoPageSuccess, options);
};

var gotoPageSuccess = function (result, argument) {
    var columnList = ["priviledgeId", "priviledgeCode", "priviledgeName"];

    cleanTableAndPage("permissionManageTable");
    var jsonData = $.parseJSON(result);
    //画table
    $("#permissionManageTable").MakeTable(false, pm_columnData, jsonData.list, tableClass, columnList, pm_operateHtml);
    //画分页
    $("#permissionManageTable").after(comPagination(argument, pageSize, jsonData.totalNumber, "gotoPage", "permissionManageP"));

    //table 操作按钮处理 修改 删除 授权
    $("#permissionManageTable button").click(function () {
        var that = $(this);
        var buttonType = that.val();
        if (buttonType === "authorize") {
            permissionAuthorization(that);
        } else if (buttonType === "delete") {
            deletePermission(that);
        } else if (buttonType === "modify") {
            modifyPermission(that);
        } else {
            alert("sometion wrong");
        }
    });
}

//增加
$("#addPermissionBtn").click(function () {
    addPermission();
});

//查询
$('#searchPermissionName').click(function () {
    gotoPage(1);
});

//授权 提交
$("#permissionSaveBtn").click(function () {
    var selectRow = $("#permissionMenuTable tbody").find("input:checked")
    var arrayList = [];

    $.each(selectRow, function (i) {
        arrayList.push($(selectRow[i]).closest("tr").find("td div")[1].innerHTML);
    });
    var priviledgeId = $("#permissionIdDisabled").text();
    var postData = {"menuId": arrayList, "priviledgeId": priviledgeId};
    var url = webPath + "/crm/savePriviledgeByMenu.do";
    var options = {"successMessage": "授权成功!"};
    postAjax(url, postData, permissionSaveSuccess, options);
});

var permissionSaveSuccess = function () {
    $("#permissionManageModal").modal('hide');
    gotoPage(getCurrentPage("permissionManageTable"));
}

//增加权限
var addPermission = function () {
    $("#permissionAddModal").modal('show');
};

var permissionAddSave = function () {
    if ($("#addPermissionForm").valid()) {
        var url = webPath + "/crm/savePriviledgeById.do";
        var postData = $("#addPermissionForm").serialize();
        var options = {"successMessage": "添加成功!"};
        postAjax(url, postData, permissionAddSaveSuccess, options);
    }
}

var permissionAddSaveSuccess = function (result) {
    $("#permissionAddModal").modal('hide');
    gotoPage(getCurrentPage("permissionManageTable"));
}

var permissionModifySave = function () {
    var postData = $("#modifyPermissionForm").serialize();
    postData.priviledgeId = $("#priviledgeId").val();
    var url = webPath + "/crm/updatePriviledgeById.do";
    var options = {"successMessage": "修改成功!"};
    postAjax(url, postData, permissionModifySaveSuccess, options);
}

var permissionModifySaveSuccess = function (result) {
    $("#permissionModifyModal").modal('hide');
    gotoPage(getCurrentPage("permissionManageTable"));
}

//修改权限
var modifyPermission = function (that) {
    var tds = that.closest("tr").find("td div");
    var priviledgeId = tds[0].innerHTML;
    var priviledgeCode = tds[1].innerHTML;
    var priviledgeName = tds[2].innerHTML;
    $("#priviledgeId").val(priviledgeId);
    $("#priviledgeCode").val(priviledgeCode);
    $("#priviledgeName").val(priviledgeName);
    $("#permissionModifyModal").modal('show');
}

//删除权限
var deletePermission = function (that) {
    var priviledgeId = getPermissionId(that);
    var priviledgeName = that.closest("tr").find("td div")[2].innerHTML;
    var url = webPath + "/crm/deletePriviledgeById.do";
    showConfirm('你确定要删除“' + priviledgeName + '”权限吗？', function () {
        var options = {"passArguments": that, "successMessage": "删除成功！"}
        postAjax(url, {"priviledgeId": priviledgeId}, deletePermissionSuccess, options);
    });
    showDialogueBox({
        'id': 'confirmMsg',
        'type': 'confirm',
        'title': '删除提示',
        'message': '你确定要删除“' + priviledgeName + '”权限吗？',
        'buttons': {
            'Yes': {
                'class': 'btn-primary',
                'action': function () {
                    var options = {"passArguments": that, "successMessage": "删除成功！"}
                    postAjax(url, {"priviledgeId": priviledgeId}, deletePermissionSuccess, options);
                }
            },
            'no': {
                'class': 'btn-secondary',
                'action': function () {

                }
            }
        }
    });
}

var deletePermissionSuccess = function (result, that) {
    gotoPage(getCurrentPage(that.closest("table").attr("id")));
}

//授权权限
var permissionAuthorization = function (that) {
    var priviledgeId = getPermissionId(that);
    var url = webPath + "/crm/getPriviledgeByMenu.do";
    var postData = {"priviledgeId": priviledgeId};
    postAjax(url, postData, permissionAuthorizationSuccess);
}

var permissionAuthorizationSuccess = function (result) {
    var columnList = ["menuId", "menuName", "menuUrl"];
    cleanTableAndPage("permissionMenuTable");
    var jsonData = $.parseJSON(result);
    $("#permissionNameDisabled").text(jsonData.object.priviledgeName);
    $("#permissionIdDisabled").text(jsonData.object.priviledgeId);
    $("#permissionCodeDisabled").text(jsonData.object.priviledgeCode);
    $("#permissionMenuTable").MakeTable(true, pme_columnData, jsonData.list, tableClass, columnList);
    //多选 选中
    var tdList = $("#permissionMenuTable tbody td:nth-child(2)");
    var selectMenuList = jsonData.object.priviledgeMenus;
    if (tdList && tdList.length > 0) {
        $.each(selectMenuList, function (i) {
            tableCheckBoxChecked(tdList, selectMenuList[i].menuId + "");
        });
    }
    $("#title-permission-table-checkbox").click(function () {
        var that = this;
        checkBoxSelectAll(that);
    });
    $("#permissionManageModal").modal('show');
}


var getPermissionId = function (that) {
    return that.closest("tr").find("td div")[0].innerHTML;
}
